﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using DHJ.Cost.Analysis.Dto;
using DHJ.Cost.CostAccounting.Cache;
using DHJ.Cost.Workshops;

namespace DHJ.Cost.Analysis
{
    public class AnalysisAppService : CostAppServiceBase, IAnalysisAppService
    {
        private readonly CostAccountingCache _costAccountingCache;
        private List<string> _costTypes;

        public AnalysisAppService(CostAccountingCache costAccountingCache)
        {
            _costAccountingCache = costAccountingCache;
            _costTypes = new List<string>()
            {
                "原材料成本",
                "辅助材料成本",
                "标准件成本",
                "外协产品成本",
                "工装成本",
                "燃动成本",
                "人工成本",
                "制造成本",
                "其他直接成本"
            };
        }

        public async Task<ListResultDto<AnnualCostDto>> GetAnnualCost()
        {
            var currentYearCosts = await _costAccountingCache.GetAnnualCacheItems(DateTime.Now.Year);
            var lastYearCosts = await _costAccountingCache.GetAnnualCacheItems(DateTime.Now.Year - 1);

            var costs = new List<AnnualCostDto>();

            foreach (var type in _costTypes)
            {
                var cost = new AnnualCostDto()
                {
                    CostType = type
                };

                var currentYearCost = currentYearCosts.FirstOrDefault(e => e.Category == type);
                var lastYearCost = lastYearCosts.FirstOrDefault(e => e.Category == type);

                if (currentYearCost != null)
                {
                    cost.CurrentYearCost = currentYearCost.Cost / 10000;
                }

                if (lastYearCost != null)
                {
                    cost.LastYearCost = lastYearCost.Cost / 10000;
                }

                cost.DifferCost = cost.CurrentYearCost - cost.LastYearCost;

                if (cost.CurrentYearCost + cost.LastYearCost != 0)
                {
                    cost.CurrentYearPercentage =
                        cost.CurrentYearCost / (cost.CurrentYearCost + cost.LastYearCost) * 100;
                    cost.LastYearPercentage = cost.LastYearCost / (cost.CurrentYearCost + cost.LastYearCost) * 100;
                }

                if (cost.CurrentYearCost != 0)
                    cost.DifferPercentage = cost.DifferCost / cost.CurrentYearCost * 100;

                costs.Add(cost);
            }

            return new ListResultDto<AnnualCostDto>(costs);
        }


        public async Task<ListResultDto<WorkshopCostDto>> GetWorkshopCost()
        {
            var workshops = Enum.GetValues(typeof(Workshop));


            var costs = new List<WorkshopCostDto>();

            foreach (var work in workshops)
            {
                var workshop = (Workshop) Convert.ToInt32(work);

                var items = await _costAccountingCache.GetWorkshopCacheItems(workshop);

                var cost = new WorkshopCostDto
                {
                    Workshop = workshop,
                };
                var item = items.FirstOrDefault(e => e.Category == "原材料成本");
                cost.原材料成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "辅助材料成本");
                cost.辅助材料成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "标准件成本");
                cost.标准件成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "外协产品成本");
                cost.外协产品成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "工装成本");
                cost.工装成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "燃动成本");
                cost.燃动成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "人工成本");
                cost.人工成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "制造成本");
                cost.制造成本 = item?.Cost / 10000 ?? 0;

                item = items.FirstOrDefault(e => e.Category == "其他直接成本");
                cost.其他直接成本 = item?.Cost / 10000 ?? 0;


                costs.Add(cost);
            }

            return new ListResultDto<WorkshopCostDto>(costs);
        }
    }
}